package {{packageName}}.shared.pagination.infrastructure.primary;

import static org.assertj.core.api.Assertions.*;
import static {{packageName}}.shared.pagination.domain.{{baseName}}PagesFixture.*;

import java.util.function.Function;
import org.junit.jupiter.api.Test;
import {{packageName}}.JsonHelper;
import {{packageName}}.UnitTest;
import {{packageName}}.shared.error.domain.MissingMandatoryValueException;

@UnitTest
class Rest{{baseName}}PageTest {

  @Test
  void shouldNotConvertWithoutSourcePage() {
    assertThatThrownBy(() -> Rest{{baseName}}Page.from(null, source -> "test")).isExactlyInstanceOf(
      MissingMandatoryValueException.class
    );
  }

  @Test
  void shouldNotConvertWithoutMappingFunction() {
    assertThatThrownBy(() -> Rest{{baseName}}Page.from(page(), null)).isExactlyInstanceOf(
      MissingMandatoryValueException.class
    );
  }

  @Test
  void shouldMapFromDomainPage() {
    Rest{{baseName}}Page<String> page = Rest{{baseName}}Page.from(page(), Function.identity());

    assertThat(page.getContent()).containsExactly("test");
    assertThat(page.getCurrentPage()).isEqualTo(2);
    assertThat(page.getPageSize()).isEqualTo(10);
    assertThat(page.getTotalElementsCount()).isEqualTo(21);
    assertThat(page.getPagesCount()).isEqualTo(3);
  }

  @Test
  void shouldGetPageCountForPageLimit() {
    Rest{{baseName}}Page<String> page = Rest{{baseName}}Page.from(
      pageBuilder().totalElementsCount(3).pageSize(3).build(),
      Function.identity()
    );

    assertThat(page.getPagesCount()).isEqualTo(1);
  }

  @Test
  void shouldSerializeToJson() {
    assertThat(JsonHelper.writeAsString(Rest{{baseName}}Page.from(page(), Function.identity()))).isEqualTo(json());
  }

  private String json() {
    return """
    {"content":["test"],\
    "currentPage":2,\
    "pageSize":10,\
    "totalElementsCount":21,\
    "pagesCount":3,\
    "hasPrevious":true,\
    "hasNext":false\
    }\
    """;
  }
}
